package com.xxl.job.executor.service.business;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import com.xxl.job.core.util.ListUtils;
import com.xxl.job.executor.model.WebReportRank;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 公用业务
 */
public class BaseService {

    /**
     * 返回
     * @param aggregationResults
     * @param n
     * @return
     */
    public List<DBObject> getMongoTopN(AggregationResults<BasicDBObject> aggregationResults, int n) {
        List<DBObject> list = new ArrayList<>();
        for (Iterator<BasicDBObject> iterator = aggregationResults.iterator(); iterator.hasNext(); ) {
            DBObject obj = iterator.next();
            list.add(obj);
        }
        list = sortByDes(list);

        /*List<DBObject> newList = new ArrayList<>();

        for(int i = 1; i <= list.size(); i++) {//取前n个
            if(i == n+1) break;
            if(list.get(i-1) != null && list.get(i-1).get("_id") != null) {
                newList.add(list.get(i-1));
            }
        }*/

        return list;
    }

    /**
     * 按统计数量大小倒序
     * @param list
     * @return
     */
    public List<DBObject> sortByDes(List<DBObject> list) {
        if(ListUtils.isNotEmpty(list)) {
            Collections.sort(list, new Comparator<DBObject>() {//倒序排序，从大到小
                @Override
                public int compare(DBObject o1, DBObject o2) {
                    int sum1 = (int) o1.get("count");
                    int sum2 = (int) o2.get("count");
                    return (sum1 <= sum2 ? 1 : -1);
                }
            });
        }

        return list;
    }

    /**
     * 流动人口top5
     * @param list
     * @return
     */
    public List<Map<String, Object>> sort(List<Map<String, Object>> list) {
        if(ListUtils.isNotEmpty(list)) {
            Collections.sort(list, new Comparator<Map<String, Object>>() {//倒序排序，从大到小
                @Override
                public int compare(Map<String, Object> map1, Map<String, Object> map2) {
                    long sum1 = (long) map1.get("value");
                    long sum2 = (long) map2.get("value");
                    return (sum1 <= sum2 ? 1 : -1);
                }
            });
        }

        return list;
    }

    /**
     * 获取topN
     * @param list
     * @param topN
     * @return
     */
    public List<Map<String, Object>> getTopN(List<Map<String, Object>> list, int topN) {
        List<Map<String, Object>> newList = new ArrayList<>();
        int currentI = 0;
        int index = 1;
        for (int i = 0; i < list.size() && list.size() >= currentI; i++) {
            if (index > topN) {
                break;
            }
            currentI = i + 1;
            Map<String, Object> map = list.get(i);
            if(!CollectionUtils.isEmpty(map)) {
                newList.add(map);
                index += 1;
            }
        }
        return newList;
    }

}
